package com.qf.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Component
public class PublisherConfirmAndReturnConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {


    @Autowired
    RabbitTemplate template;

    //在创建时 执行 init方法
    @PostConstruct
    public void init(){
        //设置return机制
        template.setReturnCallback(this);
        //设置confirm机制
        template.setConfirmCallback(this);
    }



    //保证消息到达 交换机
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        //id是消息的唯一标识
//        correlationData.getId();
        if(ack) {
            System.out.println("已到达交换机");
        }else {
            System.out.println(cause);
            System.out.println("消息未到达交换机");

        }
    }

    //保证消息 从交换机转发到达队列
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("消息未到达队列");
    }
}
